/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FNewMedia.java
 *
 * Created on 15.12.2011, 15:54:38
 */
package de.haw.dbp.a4.gui;

import dc.connectionmanager.database.DBConnectionManager;
import dc.connectionmanager.logic.ICallback;
import de.haw.dbp.a4.logic.DBCommand;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

/**
 *
 * @author jmenzel
 */
public class FNewMedia extends javax.swing.JFrame
{
    private ADBListPanel parent;
    private DBConnectionManager dbcm;

    /** Creates new form FNewMedia */
    public FNewMedia(ADBListPanel rParent, DBConnectionManager rDBCM)
    {
        this.parent = rParent;
        this.dbcm = rDBCM;
        
        initComponents();
        
        ((DBCommand)this.dbcm.getCommandClass()).getAllMedienTyp(new ICallback() 
        {

            @Override
            public void handleResult(Object result)
            {
                if(result != null)
                {
                    fillMTypCombobox((ArrayList<ArrayList<String>>) result);
                }
            }
        });
        
        this.setVisible(true);
    }
    
    private void fillMTypCombobox(ArrayList<ArrayList<String>> data)
    {
        cb_mTyp.removeAllItems();
        if(data.size() > 0)
        {
            for(ArrayList<String> list : data)
            {
                if(list != data.get(0))
                {
                    cb_mTyp.addItem(list.get(0));
                }
                
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        p_content = new javax.swing.JPanel();
        l_title = new javax.swing.JLabel();
        l_mName = new javax.swing.JLabel();
        tf_mName = new javax.swing.JTextField();
        l_mHersteller = new javax.swing.JLabel();
        tf_mHersteller = new javax.swing.JTextField();
        l_mTyp = new javax.swing.JLabel();
        cb_mTyp = new javax.swing.JComboBox();
        b_save = new javax.swing.JToggleButton();
        b_cancel = new javax.swing.JToggleButton();
        l_error = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        l_title.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
        l_title.setText("Neues Medium anlegen");

        l_mName.setLabelFor(tf_mName);
        l_mName.setText("Name");
        l_mName.setToolTipText("");

        l_mHersteller.setLabelFor(tf_mHersteller);
        l_mHersteller.setText("Hersteller");

        l_mTyp.setLabelFor(cb_mTyp);
        l_mTyp.setText("Medientyp");

        b_save.setText("Speichern");
        b_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_saveActionPerformed(evt);
            }
        });

        b_cancel.setText("Abbrechen");
        b_cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_cancelActionPerformed(evt);
            }
        });

        l_error.setForeground(new java.awt.Color(218, 0, 0));

        javax.swing.GroupLayout p_contentLayout = new javax.swing.GroupLayout(p_content);
        p_content.setLayout(p_contentLayout);
        p_contentLayout.setHorizontalGroup(
            p_contentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_contentLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(p_contentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(p_contentLayout.createSequentialGroup()
                        .addComponent(l_error, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(p_contentLayout.createSequentialGroup()
                        .addGroup(p_contentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(l_title)
                            .addGroup(p_contentLayout.createSequentialGroup()
                                .addGroup(p_contentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(l_mName)
                                    .addComponent(l_mHersteller)
                                    .addComponent(l_mTyp))
                                .addGap(35, 35, 35)
                                .addGroup(p_contentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cb_mTyp, 0, 229, Short.MAX_VALUE)
                                    .addComponent(tf_mHersteller, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                                    .addComponent(tf_mName, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                                    .addGroup(p_contentLayout.createSequentialGroup()
                                        .addComponent(b_save)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                                        .addComponent(b_cancel)))))
                        .addContainerGap(24, Short.MAX_VALUE))))
        );
        p_contentLayout.setVerticalGroup(
            p_contentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_contentLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(l_title)
                .addGap(26, 26, 26)
                .addGroup(p_contentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_mName)
                    .addComponent(tf_mName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(p_contentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_mHersteller)
                    .addComponent(tf_mHersteller, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(p_contentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_mTyp)
                    .addComponent(cb_mTyp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(l_error, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(p_contentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b_save)
                    .addComponent(b_cancel))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(p_content, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(p_content, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void b_cancelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_b_cancelActionPerformed
    {//GEN-HEADEREND:event_b_cancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_b_cancelActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt)//GEN-FIRST:event_formWindowClosing
    {//GEN-HEADEREND:event_formWindowClosing
        this.dispose();
    }//GEN-LAST:event_formWindowClosing

    private void b_saveActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_b_saveActionPerformed
    {//GEN-HEADEREND:event_b_saveActionPerformed
        boolean check = true;
        
        if(tf_mName.getText().length() == 0)
        {
            check = false;
        }
        
        if(tf_mHersteller.getText().length() == 0)
        {
            check = false;
        }
        
        if(!check)
        {
            this.l_error.setText("Die Eingabe ist nicht korrekt!");
        }
        else
        {
            ((DBCommand)this.dbcm.getCommandClass()).insertNewMedium(tf_mName.getText(), tf_mHersteller.getText(), cb_mTyp.getSelectedIndex() + 1);
            this.parent.reloadData();
            this.dispose();
        }
        
        
    }//GEN-LAST:event_b_saveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton b_cancel;
    private javax.swing.JToggleButton b_save;
    private javax.swing.JComboBox cb_mTyp;
    private javax.swing.JLabel l_error;
    private javax.swing.JLabel l_mHersteller;
    private javax.swing.JLabel l_mName;
    private javax.swing.JLabel l_mTyp;
    private javax.swing.JLabel l_title;
    private javax.swing.JPanel p_content;
    private javax.swing.JTextField tf_mHersteller;
    private javax.swing.JTextField tf_mName;
    // End of variables declaration//GEN-END:variables

}
